package com.cskaoyan.service;

import com.cskaoyan.bean.po.*;
import com.cskaoyan.bean.vo.WXHomeIndexVo;
import com.cskaoyan.mapper.*;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WXHomeIndexServiceImpl implements WXHomeIndexService{
    @Autowired
    MarketGoodsMapper marketGoodsMapper;
    @Autowired
    MarketCouponMapper marketCouponMapper;
    @Autowired
    MarketCategoryMapper marketCategoryMapper;
    @Autowired
    MarketAdMapper marketAdMapper;
    @Autowired
    MarketBrandMapper marketBrandMapper;
    @Autowired
    MarketTopicMapper marketTopicMapper;
    @Autowired
    MarketSystemMapper marketSystemMapper;

    @Override
    public WXHomeIndexVo getIndexVo() {
        WXHomeIndexVo indexVo = new WXHomeIndexVo();
        //先查表market_system获取分页信息
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        marketSystemExample.createCriteria().andKeyNameLike("market_wx_%");
        List<MarketSystem> marketSystems = marketSystemMapper.selectByExample(marketSystemExample);
        //挨个获取各个表的分页信息：
        //新品首发limit取值
        String indexNewLimt = null;
        //人气推荐栏目商品数量显示
        String indexHot = null;
        //品牌制作商直供栏目数量显示
        String indexBrand = null;
        //专题精选
        String indexTopic = null;
        //分类栏目
        String catlogList = null;
        //分类栏目商品
        String catlogGoods = null;
        for (MarketSystem marketSystem : marketSystems) {
            if("market_wx_index_new".equals(marketSystem.getKeyName())){
                indexNewLimt = marketSystem.getKeyValue();
            }
            if("market_wx_index_hot".equals(marketSystem.getKeyName())){
                indexHot = marketSystem.getKeyValue();
            }
            if("market_wx_index_brand".equals(marketSystem.getKeyName())){
                indexBrand = marketSystem.getKeyValue();
            }
            if("market_wx_index_topic".equals(marketSystem.getKeyName())){
                indexTopic = marketSystem.getKeyValue();
            }
            if("market_wx_catlog_list".equals(marketSystem.getKeyName())){
                catlogList = marketSystem.getKeyValue();
            }
            if("market_wx_catlog_goods".equals(marketSystem.getKeyName())){
                catlogGoods = marketSystem.getKeyValue();
            }
        }

        //表1
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        assert indexNewLimt != null;
        PageHelper.startPage(1, Integer.parseInt(indexNewLimt));
        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExampleWithBLOBs(marketGoodsExample);
        for (MarketGoods marketGood : marketGoods) {
            indexVo.setNewGoodsListEntity(marketGood);
        }
        //表2
        MarketCouponExample marketCouponExample = new MarketCouponExample();
        List<MarketCoupon> marketCoupons = marketCouponMapper.selectByExample(marketCouponExample);
        for (MarketCoupon marketCoupon : marketCoupons) {
            indexVo.setCouponListEntity(marketCoupon);
        }

        //表3
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        marketCategoryExample.createCriteria().andPidEqualTo(0);
        assert catlogList != null;
        PageHelper.startPage(1, Integer.parseInt(catlogList));
        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(marketCategoryExample);
        for (MarketCategory marketCategory : marketCategories) {
            indexVo.setChannelEntity(marketCategory);
        }

        //表4
        MarketAdExample marketAdExample = new MarketAdExample();
        List<MarketAd> marketAds = marketAdMapper.selectByExample(marketAdExample);
        for (MarketAd marketAd : marketAds) {
            indexVo.setBannerEntity(marketAd);
        }

        //表5
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        assert indexBrand != null;
        PageHelper.startPage(1, Integer.parseInt(indexBrand));
        List<MarketBrand> marketBrands = marketBrandMapper.selectByExample(marketBrandExample);
        for (MarketBrand marketBrand : marketBrands) {
            indexVo.setBrandListEntity(marketBrand);
        }

        //表6
        MarketGoodsExample marketGoodsExampleHot = new MarketGoodsExample();
        marketGoodsExampleHot.createCriteria().andIsHotEqualTo(true);
        assert indexHot != null;
        PageHelper.startPage(1, Integer.parseInt(indexHot));
        List<MarketGoods> marketGoodsHot = marketGoodsMapper.selectByExampleWithBLOBs(marketGoodsExampleHot);
        for (MarketGoods goods : marketGoodsHot) {
            indexVo.setHotGoodsListEntity(goods);
        }

        //表7
        MarketTopicExample marketTopicExample = new MarketTopicExample();
        assert indexTopic != null;
        PageHelper.startPage(1, Integer.parseInt(indexTopic));
        List<MarketTopic> marketTopics = marketTopicMapper.selectByExampleWithBLOBs(marketTopicExample);
        for (MarketTopic marketTopic : marketTopics) {
            indexVo.setTopicListEntity(marketTopic);
        }

        //表8
        MarketCategoryExample marketCategoryExampleGoods = new MarketCategoryExample();
        PageHelper.startPage(1, Integer.parseInt(catlogGoods));
        List<MarketCategory> marketCategoriesGoods = marketCategoryMapper.selectByExample(marketCategoryExampleGoods);
        for (MarketCategory marketCategoriesGood : marketCategoriesGoods) {
            Integer categoryId = marketCategoriesGood.getId();
            MarketGoodsExample marketGoodsExampleList = new MarketGoodsExample();
            marketGoodsExampleList.createCriteria().andCategoryIdEqualTo(categoryId);
            List<MarketGoods> marketGoodsList = marketGoodsMapper.selectByExampleWithBLOBs(marketGoodsExampleList);
            indexVo.setFloorGoodsListEntity(marketCategoriesGood,marketGoodsList);
        }
        return indexVo;
    }
}
